<template>
  <a-spin :spinning="confirmLoading" style="background: #fff">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNo">
                  <a-input disabled v-model="model.billNo" placeholder="请输入单据编号"  ></a-input>
                </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="盘点人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
              <select-user-by-dep :disabled=formDisabled 
                  v-model="model.employeeId"  placeholder="请输入盘点人"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departmentId">
              <j-select-depart :disabled="formDisabled" v-model="model.departmentId"  placeholder="请输入所属部门" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="盘点时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkDate">
              <j-date :disabled=formDisabled  placeholder="请选择盘点时间"  v-model="model.checkDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustType">
              <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.adjustType"
                    dictCode="adjust_type"
                    placeholder="请输入调整状态"
                  />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="调整时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustmentDate">
              <j-date :disabled=formDisabled placeholder="请选择调整时间" :show-time="true"   date-format="YYYY-MM-DD HH:mm:ss"  v-model="model.adjustmentDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col> -->
        </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="仓库明细">
              <ErpPurchaseWarehouseCheckList  ref="warehouseCheck"></ErpPurchaseWarehouseCheckList>
          </a-collapse-panel>
        </a-collapse>
      </a-form-model>
      <common-flow-list v-if="formBpm && model.bpmStatus !== '1'" :formData="formData" style="margin-top: 10px"></common-flow-list>
    </j-form-container>
    <a-row :gutter="48">
      <a-col :span="24" style="text-align: center;margin-top: 15px">
        <a-button type="primary" v-if="formBpm" :style="{ marginRight: showFlowSubmitButton?'10px':'0px' }"  @click="onPrint">打印</a-button>
        <a-button @click="submitForm('submit')" v-if="showFlowSubmitButton">提 交</a-button>
      </a-col>
    </a-row>
    <ErpPurchaseWarehouseCheckBillFormPrint
      v-if="formBpm"
      id="storehouseContent"
      class="print_style"
      :formData="model"
      :formDataHistory="formData"
    ></ErpPurchaseWarehouseCheckBillFormPrint>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import { validateDuplicateValue } from '@/utils/util'
  import ErpPurchaseWarehouseCheckList from './../ErpPurchaseWarehouseCheckList.vue'
  import CommonFlowList from '@/views/components/CommonFlowList.vue';
  import Print from '@/utils/print/printarea.js';
  import ErpPurchaseWarehouseCheckBillFormPrint from './ErpPurchaseWarehouseCheckBillFormPrint';
  export default {
    name: 'ErpPurchaseWarehouseCheckBillForm',
    components: {
      SelectUserByDep,
      ErpPurchaseWarehouseCheckList,
      CommonFlowList,
      ErpPurchaseWarehouseCheckBillFormPrint
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        activeKey: ['1','2'],
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/add",
          edit: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/edit",
          queryById: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/queryById",
          gainBillNo: '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/gainBillNo'
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      getBillNo () {
        const that = this
        getAction(this.url.gainBillNo).then((res)=>{
          if(res.success){
            that.model.billNo = res.result//单据编号
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
        this.getBillNo()
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        if(record.id){
          this.$refs.warehouseCheck.showBpm(record.id,true);
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },

      // 点击打印
      onPrint () {
        let print = new Print({
          el: '#storehouseContent',
          popTitle: '仓库盘点详情 · 禾美环保',
          endCallback() {

          }
        })
      }
    }
  }
</script>
<style>
@import "../../../../../utils/print.css";
</style>